package com.hdy.javachatroomdemo.Config;

import com.hdy.javachatroomdemo.Controller.WebSocketController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;
//这个类用来注册拦截器,交给spring进行管理后会自动执行registerWebSocketHandlers方法
@Configuration
public class WebSocketConfig implements WebSocketConfigurer {
    @Autowired
    private WebSocketController webSocketController;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        //.addInterceptors添加拦截器,把httpSession在setAttribute的时候也给websocketSession拷贝一份
        registry.addHandler(webSocketController, "/message")
                .addInterceptors(new HttpSessionHandshakeInterceptor());
    }
}
